﻿using Infrastructures;
using Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;

namespace IRepository
{
    public enum MethodType { Create, Update, Delete, Get, List }

    public class MethodHelper
    {
        public static void ExecuteMethod<T>(MethodType methodName, T obj)
        {
            Type type = FactoryCreator.Create(obj.GetType().Name).GetType();
            MethodInfo method = type.GetMethod(methodName.ToString());
            if (method == null)
                return;

            try
            {
                method.Invoke(Activator.CreateInstance(type, true), new object[] { obj });
            }
            catch (Exception ex)
            {
                CreateForException(ex, "system");
            }
        }

        public static object ExecuteMethod(MethodType methodName, Type obj, params object[] objs)
        {
            Type type = FactoryCreator.Create(obj.Name).GetType();
            MethodInfo method = type.GetMethod(methodName.ToString());
            if (method == null)
                return null;

            try
            {
                if (objs == null || objs.Length == 0)
                    return method.Invoke(Activator.CreateInstance(type), null);
                else
                    return method.Invoke(Activator.CreateInstance(type), objs);
            }
            catch (Exception ex)
            {
                CreateForException(ex, "system");
            }

            return null;
        }

        private static void Create(string oper, string message)
        {
            ILogRepository repo = FactoryCreator.Create<ILogRepository>();
            EventLog log = new EventLog();
            log.ActionType = EventType.SysError;
            log.Operator = oper;
            log.Remark = message;
            log.EventID = SystemConfig.Get("SysEventID");
            log.ActionObject = EventType.SysError.ToString();
            log.ActionTime = DateTime.Now;
            log.InstanceID = SystemConfig.Get("InstanceID");
            repo.Create(log);
        }

        private static void CreateForException(Exception ex, string oper, string url = "")
        {
            StringBuilder sb = new StringBuilder(2000);
            sb.Append("Message: " + ex.Message);

            if (!string.IsNullOrWhiteSpace(url))
                sb.Append("Url: " + url);

            if (ex.InnerException != null)
            {
                sb.Append(" InnerException.Message: " + ex.InnerException.Message);
                sb.Append(" StackTrace: " + ex.InnerException.StackTrace);
            }

            if (sb.Length > 4000)
                sb.Remove(3999, sb.Length);

            Create(oper, sb.ToString());
        }
    }
    public enum AppConfigType { SingleValue, MultiValue }

    public enum MultiValueType
    {

    }

    public enum SingleValueType
    {
        CompanySelectLimitMultiValue
    }
}
